package com.businesswebsite.businesswebsite.FormalWebExplot.Service.impl.User;

import com.businesswebsite.businesswebsite.FormalWebExplot.Dao.User.CompanyUserDao;
import com.businesswebsite.businesswebsite.FormalWebExplot.Entity.CompanyUser;
import com.businesswebsite.businesswebsite.FormalWebExplot.Service.User.CompanyUserService;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.Renum;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;

@Service
public class CompanyUserImpl implements CompanyUserService {
    @Autowired
    private CompanyUserDao companyUserDao;
//    登录
    @Override
    public CompanyUser Login(String user_account,String password) {
        if(user_account == null || password == null){
            throw new RuntimeException("账号或密码不能为空");
        }else{
//            密码md5加密
//            String  md5Password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
            CompanyUser login = companyUserDao.Login(user_account,password);
            if(login == null){
                throw new RuntimeException(Renum.USER_PASSWORD_ERROR.getCode()+":"+Renum.USER_PASSWORD_ERROR.getMsg());
            }else {
                Timestamp last_time = new Timestamp(System.currentTimeMillis());
                companyUserDao.UpdateLastLoginTime(user_account, last_time);
                return login;
            }
        }
    }
//  注册用户
//  创建事务  回滚
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer Register(CompanyUser companyUser) {
//        检查是否存在此账户 存在则返回null 不存在则返回false
        String user_account = companyUser.getUser_account();
        CompanyUser companyUser1 = SelectByAccount(user_account);
        if(companyUser1 == null){
//            密码md5加密 自动补0
            String  md5Password = DigestUtils.md5DigestAsHex(companyUser.getPassword().getBytes(StandardCharsets.UTF_8));
            companyUser.setPassword(md5Password);
            Integer register = companyUserDao.Register(companyUser);
            return register;
        }else {
            throw new RuntimeException(Renum.USER_IS_EXISTS.getCode()+":"+Renum.USER_IS_EXISTS.getMsg());
        }
    }
// 更新用户信息
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer Update(CompanyUser companyUser) {
        int update = companyUserDao.Update(companyUser);
        return update;
    }
// 通过账号查看用户是否存在 用于查重注册
    @Override
    public CompanyUser SelectByAccount(String user_account) {
        CompanyUser user = companyUserDao.SelectByAccount(user_account);
        return user;
    }
//更新上次登录时间
    @Override
    public Integer UpdateLastLoginTime(String user_account, Timestamp last_login_time) {
        Integer updateLastLoginTime = companyUserDao.UpdateLastLoginTime(user_account,last_login_time);
        return updateLastLoginTime;
    }
//注销用户
    @Override
    public Integer Delete(String user_account) {
        Integer delete = companyUserDao.Delete(user_account);
        return delete;
    }
}
